package com.cskaoyan.service;

import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.model.MarketGoodsProduct;
import com.cskaoyan.model.MarketGoodsProductExample;
import com.cskaoyan.model.MarketOrderGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/13
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    MarketGoodsProductMapper productMapper;

    @Override
    public MarketGoodsProduct  finById(Integer productId) {
        MarketGoodsProductExample example = new MarketGoodsProductExample();
        example.createCriteria()
                .andIdEqualTo(productId)
                .andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectByExample(example);
        return marketGoodsProducts!=null&&marketGoodsProducts.size()==1?marketGoodsProducts.get(0):null;
    }

    @Override
    public boolean deduceGoods(Integer goodsId, Short orderNumber) {
        //3.在marketgoodsproduct表中减去商品数量--更新操作
        MarketGoodsProductExample example = new MarketGoodsProductExample();
        example.createCriteria()
                .andGoodsIdEqualTo(goodsId)
                .andDeletedEqualTo(false);
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectByExample(example);
        if (marketGoodsProducts!=null&&marketGoodsProducts.size()==1){
            MarketGoodsProduct product = marketGoodsProducts.get(0);
            Integer number = product.getNumber();
            int afterDelNum = number - orderNumber;
            if (afterDelNum <0){
               return false;
            }else {
                product.setNumber(afterDelNum);
                product.setUpdateTime(new Date());
                productMapper.updateByExampleSelective(product,example);
            }
        }
        return true;
    }
}
